/**
 * @author helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.CompanyDeptDao) SALE.dao.local.CompanyDeptDao = function(){
	var companyDoc;
}

SALE.dao.local.CompanyDeptDao.prototype = {
		/**
		 * 
		 */
		initCompanyDoc : function() {
			if(!this.companyDoc) {
				this.companyDoc = LocalXMLFileUtil.getCompanyDoc();
			}
			
			return this.companyDoc;
		},
		
		saveCompanyDoc : function() {
			LocalXMLFileUtil.saveXML(this.companyDoc,LocalXMLFileUtil.getCompanyFile());
		},
		
		insertDeptInfo : function(deptJson) {
			var root = this.companyDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			deptJson["id"] = id;
			root.setAttribute("maxid",id);
			var deptNode = this.companyDoc.createElement("dept");
			
			this.updateDept(deptJson, deptNode);			
			root.appendChild(deptNode);
			
			this.saveCompanyDoc();
		},
		
		modifyDeptInfo : function(deptJson) {
			var deptNode = this.searchDeptByID(deptJson.id);
			
			this.updateDept(deptJson, deptNode);
			
			this.saveCompanyDoc();
		},
		
		deleteDeptInfo : function(id) {
			var deptNode = this.searchDeptByID(id);
			var root = this.companyDoc.documentElement;
			root.removeChild(deptNode);
		},
				
		updateDept : function(deptJosn, deptNode) {
			
			for(var attri in deptJosn) {	
				
				if(attri.indexOf("old") != 0 && deptJosn[attri] && deptJosn[attri] != "") {
					var attriNodes = Ext.DomQuery.select("/" + attri, deptNode);
					if(attriNodes != null && attriNodes.length > 0) {
						deptNode.removeChild(attriNodes[0]);
					}
					
					var attriNode = this.companyDoc.createElement(attri);
					var contextNode = this.companyDoc.createCDATASection(deptJosn[attri]);
					attriNode.appendChild(contextNode);
					deptNode.appendChild(attriNode);
				}
			}
		},
		
		searchDeptByID : function(id) {
			var root = this.companyDoc.documentElement;
			var objArray = Ext.query("dept:has(id:nodeValue(" + id+ "))", root);
			var result;
						
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		searchDeptJsonByID : function(id) {
			
			var resultNode = this.searchDeptByID(id);
			
			var result = {};
			
			if (resultNode != null) {
				var children = resultNode.childNodes;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];					
					if (child.nodeType == 1) {
						if (!result[child.nodeName] && child.childNodes[0] != null) {
							result[child.nodeName] = child.childNodes[0].nodeValue;
						}
					}
				}
			}
			
			return result;
		},
		
		buildDeptsJson : function() {
			var root = this.initCompanyDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/dept", root);
				var deptsJson = {depts: []};
				
				Ext.each(objArray, function(dept) {
					var children = dept.childNodes;
					var deptJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!deptJson[child.nodeName] && child.childNodes[0] != null) {
								deptJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					
					deptsJson.depts.push(deptJson);
				});
				
				return deptsJson;
			}
		}
}